package content.views;
import feffects.easing.Quad;
import haxe.Timer;
import js.Lib;
import tools.ASprite;
import tools.BoundProvider;

/**
 * ...
 * @author PRASS
 */

class Home extends BaseView
{
	
	private var _layerTop	: ASprite;
	private var _layerLow	: ASprite;
	private var _timer 		: Timer;

	
	public function new( id : String, tpl : String ) 
	{
		super(id,tpl);
	}
	
	override public function build():Void 
	{
		_layerTop = new ASprite( new JQuery("#layertop") );
		_layerTop.dom.css("position", "absolute");
		//_layerTop.setWidth(374);
		//_layerTop.setHeight(110);
		_layerTop.setAlpha(0);
		
		_layerLow = new ASprite( new JQuery("#layerlow"));
		_layerLow.dom.css("position", "absolute");
		_layerLow.setAlpha(0);
	}
	
	override public function play():Void 
	{
		super.play();
		
		_layerTop.tween(0, _layerTop.setAlpha, 1, 400, Quad.easeOut);
		_layerLow.tween(0, _layerLow.setAlpha, 1, 400, Quad.easeOut, null, 150);
		
		if (_timer != null)
		{
			_timer.stop();
			_timer = null;
		}
		
		dom.mousemove( function(e) { checkMousePos(e); } );
		
		_smoothMouseX = 0;
		
		_timer = new Timer(25);
		_timer.run = _onMove;
	}
	
	
	private var _smoothMouseX 	: Float;
	private var _smoothMouseY 	: Float;
	private var _mouseX			: Float;
	private var _mouseY			: Float;
		
	private var _initX 		: Float;
	private var _initY 		: Float;
	
	private function _onMove() : Void
	{
		// smoothed mouse position
		_smoothMouseX += ( _mouseX - _smoothMouseX ) * .3; 
		_smoothMouseY += ( _mouseY - _smoothMouseY ) * .3; 
		
		// parrallax
		_layerLow.setX( cast ( _initX  - ( _smoothMouseX / (BoundProvider.winW/23) ) ) ) ; // 70 de ratio ...
		_layerLow.setY( cast ( _initY  - ( _smoothMouseY / (BoundProvider.winH/16) ) ) ) ; // 47 de ratio...
	}
	
	private function checkMousePos(e) : Void
	{
		_mouseX = -128 + e.pageX - BoundProvider.winW *.5;// / 2;// + (getWidth() * .5);
		_mouseY = e.pageY - BoundProvider.winH*.5;
	}
	
	override public function hide():Void 
	{
		if(_layerTop != null) _layerTop.tween(1, _layerTop.setAlpha, 0, 300, Quad.easeOut, null, 150);
		if (_layerLow != null) _layerLow.tween(1, _layerLow.setAlpha, 0, 300, Quad.easeOut);
		
		dom.mousemove( null );
		
		if (_timer != null)
		{
			_timer.stop();
			_timer = null;
		}
	}
	
	override public function resize(_w:Int, _h:Int):Void 
	{
		super.resize(_w, _h);
		
		if ( _layerTop == null ) return;
		
		//_initX = ( _w + 216 - _layerTop.getWidth() ) * .5;
		_initX = 356;
		//_initY = ( _h - _layerTop.getHeight() ) * .5;
		_initY = 100;
		
		_layerTop.setX( cast _initX );
		_layerTop.setY( cast _initY );
		
		_layerLow.setX( cast _initX );
		_layerLow.setY( cast _initY );
	}
	
}